import axios from 'axios'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui';
import router from '@/router';

let service = axios.create({
    baseURL: 'http://localhost:3000',
    timeout: 10000
});


// 请求拦截器
service.interceptors.request.use(config => {
    let tokenValue = getToken();
    if (tokenValue) {
        // headers["hg_token"]
        config.headers["hg_token"] = tokenValue;
    }
    return config
});
// 响应拦截  状态码的控制 token过期重新进入登录页面等等
service.interceptors.response.use(response => {
    console.log(response.data.status);
    let status = response.data.status;
    switch (status) {
        case 200:
            return response;
        case 1001:
        case 1002:
        case 1003:
        case 1004:
        case 1005:
        case 1006:
            return response;
        case 10010:
        case 10011:
        case 401:
            Message({
                message: response.data.msg,
                type: 'warning'
            });
            router.push('/login');
            break;
        default:
            break;

    }


})


export default service;